package elite.rpt.runNew;

import elite.core.log.logCls;
import elite.core.log.logIF;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RunOneNewI18n {
    private static final logIF log = logCls.getMe(RunOneNewI18n.class);
    private static String log_Prefix = "RPT Lang Deal-";
    private static String[] skip_Keys = "binds".split(";");
    private static String[] label_Keys = "content;desc;label".split(";");

    private static JSONObject json_I18n = new JSONObject();

    public static String doRPTI18n(String rptStructStr, String rptI18nStr, String LoginLang) {
        log.debug(log_Prefix + " 翻译的目标语言:" + LoginLang + " 当前系统报表语言资源:" + rptI18nStr);
        String resStructStr = rptStructStr;
        try {

            if (!LoginLang.equals("zh_CN") && !isNull(rptI18nStr)) {
                json_I18n = new JSONObject(rptI18nStr).getJSONObject(LoginLang);
                resStructStr = analysisDyn(rptStructStr);
            }
        } catch (Exception e) {
            log.error(log_Prefix + " 报表结构翻译异常", e);
        }
        return resStructStr;
    }

    public static String analysisDyn(String str) {
        JSONObject json = null;
        JSONObject re_Json = null;
        json = new JSONObject(str);
        re_Json = new JSONObject(str);
        Set<String> keySet = json.keySet();
        for (String key : keySet) {
            if (!Arrays.asList(skip_Keys).contains(key)) {
                Object obj = json.get(key);
                if (obj instanceof JSONArray) {
                    JSONArray arr = (JSONArray) obj;
                    for (int i = 0; i < arr.length(); i++) {
                        if (arr.get(i) instanceof JSONObject) {
                            String child = analysisDyn(arr.get(i).toString());
                            arr.put(i, new JSONObject(child));
                        } else {
                            arr.put(i, arr.get(i).toString());
                        }
                    }
                    re_Json.put(key, arr);
                } else if (obj instanceof JSONObject) {
                    JSONObject sub = (JSONObject) obj;
                    String substr = analysisDyn(sub.toString());
                    re_Json.put(key, new JSONObject(substr));
                } else {
                    String key_Value = obj.toString().trim();
                    if (Arrays.asList(label_Keys).contains(key)) {

                        log.debug(log_Prefix + "#原始信息-key：" + key + " value：" + key_Value);

                        // 判断是否已经替换
                        if (!isNull(key_Value) && isContainChinese(key_Value)) {

                            // 1-处理标签中有i class
                            try {
                                if (key_Value.endsWith(":") || key_Value.endsWith("：")) {
                                    key_Value = key_Value.substring(0, key_Value.length() - 1);
                                    key_Value = (json_I18n.has(key_Value) ? json_I18n.getString(key_Value) : key_Value) + ":";
                                } else {
                                    key_Value = (json_I18n.has(key_Value) ? json_I18n.getString(key_Value) : key_Value);
                                }
                                log.debug(log_Prefix + "#语言数据-key：" + key + " value：" + key_Value);
                                re_Json.put(key, key_Value);

                            } catch (Exception e) {
                                log.debug(log_Prefix + "#语言数据-key：" + key + " value：" + key_Value + " 异常-" + e.getMessage());

                            }
                        } else {
                            // 语言字段为空或未包含中文
                            log.debug(log_Prefix + "#语言数据-key：" + key + " value：" + key_Value + " 语言字段为空或未包含中文");
                        }
                    }
                }
            } else {
                log.debug(log_Prefix + "Json数据-key：" + key + " Key跳过");

            }
        }
        return re_Json.toString();
    }

    // 非空校验isNull
    public static boolean isNull(String value) {

        boolean reFlag = true;
        if (value == null || value.length() <= 0) {
            reFlag = true;
        } else {
            reFlag = false;
        }

        return reFlag;
    }

    // 判断字符串是否含中文
    public static boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
}